package com.loveplusplus.update;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import zhexian.app.smartcall.R;
import zhexian.app.smartcall.base.BaseApplication;
import zhexian.app.smartcall.base.Const;
import zhexian.app.smartcall.lib.ZHttp;

public class UpdateChecker extends Fragment {

    //private static final String NOTIFICATION_ICON_RES_ID_KEY = "resId";
    private static final String NOTICE_TYPE_KEY = "type";
    private static final String APP_UPDATE_SERVER_URL = "app_update_server_url";
    //private static final String SUCCESSFUL_CHECKS_REQUIRED_KEY = "nChecks";
    private static final int NOTICE_NOTIFICATION = 2;
    private static final int NOTICE_DIALOG = 1;
    private static final String TAG = "UpdateChecker";

    private Activity mContext;
    private Thread mThread;
    private int mTypeOfNotice;

    /**
     * Show a Dialog if an update is available for download. Callable in a
     * FragmentActivity. Number of checks after the dialog will be shown:
     * default, 5
     *
     * @param fragmentActivity Required.
     */
    public static void checkForDialog(Activity fragmentActivity, String url) {
        FragmentTransaction content = fragmentActivity.getFragmentManager().beginTransaction();
        UpdateChecker updateChecker = new UpdateChecker();
        Bundle args = new Bundle();
        args.putInt(NOTICE_TYPE_KEY, NOTICE_DIALOG);
        args.putString(APP_UPDATE_SERVER_URL, url);
        //args.putInt(SUCCESSFUL_CHECKS_REQUIRED_KEY, 5);
        updateChecker.setArguments(args);
        content.add(updateChecker, null).commit();
    }


    /**
     * Show a Notification if an update is available for download. Callable in a
     * FragmentActivity Specify the number of checks after the notification will
     * be shown.
     *
     * @param fragmentActivity Required.
     */
    public static void checkForNotification(Activity fragmentActivity, String url) {
        FragmentTransaction content = fragmentActivity.getFragmentManager().beginTransaction();
        UpdateChecker updateChecker = new UpdateChecker();
        Bundle args = new Bundle();
        args.putInt(NOTICE_TYPE_KEY, NOTICE_NOTIFICATION);
        args.putString(APP_UPDATE_SERVER_URL, url);
        //args.putInt(NOTIFICATION_ICON_RES_ID_KEY, notificationIconResId);
        //args.putInt(SUCCESSFUL_CHECKS_REQUIRED_KEY, 5);
        updateChecker.setArguments(args);
        content.add(updateChecker, null).commit();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            Bundle args = getArguments();
            mTypeOfNotice = args.getInt(NOTICE_TYPE_KEY);
            String url = args.getString(APP_UPDATE_SERVER_URL);
            //mSuccessfulChecksRequired = args.getInt(SUCCESSFUL_CHECKS_REQUIRED_KEY);
            //mNotificationIconResId = args.getInt(NOTIFICATION_ICON_RES_ID_KEY);
            checkForUpdates(url);
        }
    }

    /**
     * This class is a Fragment. Check for the method you have chosen.
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("initFinished", false);
        super.onSaveInstanceState(outState);
    }

    /**
     * Heart of the library. Check if an update is available for download
     * parsing the desktop Play Store page of the app
     */
    private void checkForUpdates(final String url) {
        mThread = new Thread() {
            @Override
            public void run() {
                //if (isNetworkAvailable(mContext)) {

                //String json = sendPost(url);
                String json = ZHttp.getString(url);
                if (json != null) {
                    parseJson(json);
                } else {
                    Log.e(TAG, "can't get app update json");
                }
                //}
            }

        };
        mThread.start();
    }

//    protected String sendPost(String urlStr) {
//        HttpURLConnection uRLConnection = null;
//        InputStream is = null;
//        BufferedReader buffer = null;
//        String result = null;
//        try {
//            URL url = new URL(urlStr);
//            uRLConnection = (HttpURLConnection) url.openConnection();
//            uRLConnection.setDoInput(true);
//            uRLConnection.setDoOutput(false);
//            uRLConnection.setRequestMethod("GET");
//            uRLConnection.setUseCaches(false);
//            uRLConnection.setConnectTimeout(10 * 1000);
//            uRLConnection.setReadTimeout(10 * 1000);
//            uRLConnection.setInstanceFollowRedirects(true);
//            uRLConnection.setRequestProperty("Connection", "Keep-Alive");
//            uRLConnection.setRequestProperty("Charset", "UTF-8");
//            uRLConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
//            uRLConnection.setRequestProperty("Content-Type", "application/json");
//
//            uRLConnection.connect();
//
//
//            is = uRLConnection.getInputStream();
//
//            String content_encode = uRLConnection.getContentEncoding();
//
//            if (null != content_encode && !"".equals(content_encode) && content_encode.equals("gzip")) {
//                is = new GZIPInputStream(is);
//            }
//
//            buffer = new BufferedReader(new InputStreamReader(is));
//            StringBuilder strBuilder = new StringBuilder();
//            String line;
//            while ((line = buffer.readLine()) != null) {
//                strBuilder.append(line);
//            }
//            result = strBuilder.toString();
//        } catch (Exception e) {
//            Log.e(TAG, "http post error", e);
//        } finally {
//            if (buffer != null) {
//                try {
//                    buffer.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (is != null) {
//                try {
//                    is.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (uRLConnection != null) {
//                uRLConnection.disconnect();
//            }
//        }
//        return result;
//    }


    private void parseJson(String json) {
        mThread.interrupt();
        Looper.prepare();
        try {

            JSONObject obj = new JSONObject(json);
            String updateMessage = obj.getString(Constants.APK_UPDATE_CONTENT);
            String apkName = obj.getString(Constants.APK_FILE_NAME);
            int apkCode = obj.getInt(Constants.APK_VERSION_CODE);
            String apkUrl = obj.getString(Constants.APK_DOWNLOAD_URL);
            // 如果返回的信息中没有下载URl，则使用默认的下载路径
            if (apkUrl == null || !apkUrl.toLowerCase().startsWith("http")) {
                BaseApplication baseApp = (BaseApplication) this.getActivity().getApplication();
                apkUrl = baseApp.getServiceUrl(Const.URL_DOWNLOAD_APK);
            }

            int versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;

            if (apkCode > versionCode) {
                if (mTypeOfNotice == NOTICE_NOTIFICATION) {
                    showNotification(updateMessage, apkUrl, apkName);
                } else if (mTypeOfNotice == NOTICE_DIALOG) {
                    showDialog(updateMessage, apkUrl, apkName);
                }
            } else {
                //Toast.makeText(mContext, mContext.getString(R.string.app_no_new_update), Toast.LENGTH_SHORT).show();
            }

        } catch (PackageManager.NameNotFoundException ignored) {
        } catch (JSONException e) {
            Log.e(TAG, "parse json error", e);
        }
    }

    /**
     * Show dialog
     */
    public void showDialog(String content, String apkUrl, String apkName) {
        UpdateDialog d = new UpdateDialog();
        Bundle args = new Bundle();
        args.putString(Constants.APK_UPDATE_CONTENT, content);
        args.putString(Constants.APK_DOWNLOAD_URL, apkUrl);
        args.putString(Constants.APK_FILE_NAME, apkName);
        d.setArguments(args);
        d.show(mContext.getFragmentManager(), "UpdateDialog");
    }

    /**
     * Show Notification
     */
    public void showNotification(String content, String apkUrl, String fileName) {
        Notification noti;
        Intent myIntent = new Intent(mContext, DownloadService.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myIntent.putExtra(Constants.APK_DOWNLOAD_URL, apkUrl);
        myIntent.putExtra(Constants.APK_FILE_NAME, fileName);
        PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        int smallIcon = mContext.getApplicationInfo().icon;
        noti = new Notification.Builder(mContext).setTicker(getString(R.string.newUpdateAvailable))
                .setContentTitle(getString(R.string.newUpdateAvailable)).setContentText(content).setSmallIcon(smallIcon)
                .setContentIntent(pendingIntent).build();

        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);
    }


    /**
     * Check if a network available
     */
    public static boolean isNetworkAvailable(Context context) {
        boolean connected = false;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni != null) {
                connected = ni.isConnected();
            }
        }
        return connected;
    }


}
